"""
Created on Fri Sep  6 09:37:01 2013

@author: chico
"""
import sys
from PySide import QtCore, QtGui, QtWebKit

from msTreeMapperWidget import *
from msPhysicalInterfaceWidget import *
from msGeneralizedCoordinatesWidget import *
from msScalarVariableWidget import *
from msScalarFunctionWidget import *   
from msUnitsManagerWidget import *   
from msScalarFunctionWidget import *   
from msParserWidget import *
from msEntityWidget import *
from msZMatWidget import *
from msKineticOperatorWidget import *

class WidgetsContainer(QtGui.QWidget):
    
    def __init__(self,mainwin):
        super(WidgetsContainer, self).__init__()
        
        self.mainWindow   = mainwin
        mainBox = QtGui.QVBoxLayout()
        
        self.scrollArea = QtGui.QScrollArea()

        self.widgetsGroup = QtGui.QGroupBox()  
        self.vbox         = QtGui.QVBoxLayout()
        self.widgetsGroup.setLayout(self.vbox)   
        self.scrollArea.setWidget(self.widgetsGroup)        
        
        mainBox.addWidget(self.scrollArea)
        
        self.setLayout(mainBox)
        
    def changeTab(self,i):
        
        if(i==0):
            self.MainWindowContainer.createMenus()
        if(i==1):
            self.MainWindowContainer.createMenusScript()
             
        
    def update(self,myobject):       
        """
        update( self, (msTreeMapper)myobject ) -> void       
        load all the widget available associated to an object pointer
        
        myobject : pointer to the msTreeMapper derived object
        
        """
        self.clear()
        self.widgets = []
        types = myobject.getType().split(':')
        width=0
        height=0

        mapClasses={}
        for cls in msWidget.__subclasses__():
            mapClasses[ str(cls).split("'")[-2].split(".")[-1] ] =  cls 
        
        for thetype in types[0:-1]:
            widget=0
            thetype = thetype[:]+"Widget"
            try:
                #widget = globals()[thetype](myobject,self.mainWindow)
                widget = mapClasses[thetype](myobject,self.mainWindow)
                width  = max(width,widget.size().width())
                height = height + widget.size().height()
            except:
                self.mainWindow.reportException(sys)
            if widget:
                self.widgets.append(widget)       
        
        a=100
        self.widgetsGroup.setMinimumSize(width+a,height+a)
        self.widgetsGroup.setMaximumSize(width+a,height+a)        
        self.vbox.setSpacing(0)
        for widget in self.widgets:
            self.vbox.addWidget( widget, QtCore.Qt.AlignLeft )  
                    
    def clear(self):
        b = self.vbox.takeAt(0)
        while b:
            if(len(self.widgets)>0):
                self.widgets.pop(0)
            b.widget().deleteLater()
            b = self.vbox.takeAt(0)
            